package _thread_lock._synchronized;

public class SynchronizedDemo implements Runnable {

    Object object = new Object();
    Object object2 = new Object();

    @Override
    public void run() {
        try {
            aaa(object);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void aaa(Object object) throws InterruptedException {
        synchronized(object){
            Thread.sleep(3000);
            System.out.println("aaa");
        }
    }

    public synchronized void bbb() throws InterruptedException {
        Thread.sleep(3000);
        System.out.println("bbb");
    }

    public static void main(String[] args) {
        SynchronizedDemo synchronizedDemo = new SynchronizedDemo();
        Thread thread1 = new Thread(synchronizedDemo){
            @Override
            public void run() {
                try {
                    synchronizedDemo.aaa(synchronizedDemo.object);
//                    synchronizedDemo.bbb();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread1.start();

        Thread thread2 = new Thread(synchronizedDemo){
            @Override
            public void run() {
                try {
                    synchronizedDemo.aaa(synchronizedDemo.object);
//                    synchronizedDemo.bbb();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread2.start();

        Thread thread3 = new Thread(synchronizedDemo){
            @Override
            public void run() {
                try {
                    synchronizedDemo.aaa(synchronizedDemo.object2);
//                    synchronizedDemo.bbb();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread3.start();

//        SynchronizedDemo synchronizedDemo4 = new SynchronizedDemo();
//        Thread thread4 = new Thread(synchronizedDemo4);
//        thread4.start();
    }

}
